import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzUploadChangeParam, NzUploadFile } from "ng-zorro-antd/upload";
import { CommonsService } from "../../../../core/service/commons.service";
import { FileService } from "../../../sys/file.service";
import { DeviceInspectionTaskItemService } from "../device-inspection-task-edit/device-inspection-task-item.service";
import { DeviceInspectionTaskService } from "../device-inspection-task.service";

@Component({
  selector: "app-device-inspection-task-handle",
  templateUrl: "./device-inspection-task-handle.component.html",
  styles: []
})
export class DeviceInspectionTaskHandleComponent implements OnInit {


  constructor(private msgSrv: NzMessageService,
              private deviceInspectionTaskService: DeviceInspectionTaskService,
              private deviceInspectionTaskItemService: DeviceInspectionTaskItemService,
              private activateInfo: ActivatedRoute,
              public commonsService: CommonsService,
              public fileService: FileService
  ) {
    activateInfo.params.subscribe(params => {

      const operator = params.operator;

      if (operator === "view") {
        this.readonly = true;
      }

      this.loadDate(params.id);
    });

  }

  readonly = false;

  nzLoading = false;

  // 检验任务ID
  inspectionTaskId: string;
  // 检验项table数据
  inspectionTask: any = { deviceInspectionTaskDeviceList: [{ deviceInspectionTaskItemList: [] }] };

  previewImage: string | undefined = "";
  previewVisible = false;

  ngOnInit(): void {

  }

  // 加载数据
  loadDate(id) {

    this.nzLoading = true;

    this.inspectionTaskId = id;

    this.deviceInspectionTaskItemService.getByTask(this.inspectionTaskId).subscribe(res => {

      this.nzLoading = false;

      if (res.code === 1) {
        this.inspectionTask = res.data;

        let deviceInspectionTaskDeviceList = this.inspectionTask.deviceInspectionTaskDeviceList;

        deviceInspectionTaskDeviceList.forEach(taskDevice => {

          let taskItemList = taskDevice.deviceInspectionTaskItemList;

          taskItemList.forEach(taskItem => {
            const fileList = taskItem.fileList;
            fileList.forEach(file => {
                file.status = "done";
                file.url = "/api/file/view-picture/" + file.id;
              }
            );
          });
        });

      } else {
        this.msgSrv.error(res.message);
      }
    });

  }

  close(): void {
    this.commonsService.routerReplace("device/device-inspection-task");
  }

  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

  onChange($event: string): void {
    console.log($event);
  }

  // 处理附件
  saveHandler() {
    let deviceInspectionTaskDeviceList = this.inspectionTask.deviceInspectionTaskDeviceList;

    deviceInspectionTaskDeviceList.forEach(taskDevice => {

      let taskItemList = taskDevice.deviceInspectionTaskItemList;

      taskItemList.forEach(taskItem => {
        const fileList = taskItem.fileList;
        fileList.forEach(file => {
            if (file.response) {
              file.id = file.response.data.id;
            }
          }
        );
      });
    });
  }

  // 保存
  save() {

    this.saveHandler();

    this.nzLoading = true;

    this.deviceInspectionTaskItemService.saveList(this.inspectionTask).subscribe(res => {

      this.nzLoading = false;

      if (res.code === 1) {
        this.msgSrv.success("保存完成");
        this.close();
      } else {
        this.msgSrv.error(res.message);
      }

    });

  }

  // 提交
  submit() {

    this.saveHandler();

    this.nzLoading = true;

    this.deviceInspectionTaskItemService.submitList(this.inspectionTask).subscribe(res => {

      this.nzLoading = false;

      if (res.code === 1) {
        this.msgSrv.success("提交完成");
        this.close();
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  handleChange(info: NzUploadChangeParam): void {
    if (info.file.status !== "uploading") {

    }
    if (info.file.status === "done") {
      if (info.file.response.code === 1) {
        info.file.uid = info.file.response.data.id;
        this.msgSrv.success(`${info.file.name} file uploaded successfully`);
      } else {
        info.file.status = "error";
        this.msgSrv.error(`${info.file.name} file upload failed.${info.file.response.message}`);
      }

    } else if (info.file.status === "error") {
      this.msgSrv.error(`${info.file.name} file upload failed.`);
    }
  }


  handlePreview = async (file: NzUploadFile) => {

    if (file.name.endsWith("jpeg") || file.name.endsWith("jpg")
      || file.name.endsWith("png") || file.name.endsWith("svg")
      || file.name.endsWith("bmp") || file.name.endsWith("webp")) {

      this.previewImage = "/api/file/view-picture/" + file.uid;
      if (file.response) {
        this.previewImage = "/api/file/view-picture/" + file.response.data.id;
      }

      this.previewVisible = true;
    } else {
      this.msgSrv.info("只能预览图片格式内容");
    }

  };

  handleDownload = async (file: NzUploadFile) => {

    this.fileService.download(file.uid);
  };

}
